package com.block.api.controller.merchant;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.block.common.bean.CommonParam;
import com.block.common.exception.ApiException;
import com.block.common.security.util.ApiUtil;
import com.block.common.vo.ResponseVo;
import com.block.db.entity.AppEncrypt;
import com.block.merchant.service.IMerchantApplyInfoService;
import com.block.test.reqbean.merchant.ApplyInfoBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("api/merchantApply")
@Validated
public class MerchantApplyController {

    @Autowired
    IMerchantApplyInfoService merchantApplyInfoService;

    /**
     * 手机号 鉴权登陆
     */
    @RequestMapping(value = "/loginIn", method = {RequestMethod.POST, RequestMethod.GET})
    public String loginIn(@RequestAttribute(value = "tranData") JSONObject tranData,
                          @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                          @RequestAttribute(value = "uri") String uri,
                          @RequestAttribute(value = "commonParam") CommonParam commonParam,
                          @RequestAttribute(value = "startDate") Date startdate,
                          @RequestAttribute(value = "ip") String ip) throws ApiException {

        String mobile = tranData.getString("mobile");
        String verifyCode = tranData.getString("verifyCode");

        String ownerIdStr = tranData.getString("ownerId");
        String ownerId;

        if (StrUtil.isEmpty(ownerIdStr)) {
            ownerId = "1";
        } else {
            ownerId = ownerIdStr;
        }

        ResponseVo responseVo = merchantApplyInfoService.loginIn(mobile, verifyCode, ownerId);
        return ApiUtil.dealReturnJson(responseVo, appEncrypt, startdate, uri);
    }

    /**
     * 手机号 鉴权登陆
     */
    @RequestMapping(value = "/getApplyInfo", method = {RequestMethod.POST, RequestMethod.GET})
    public String getApplyInfo(@RequestAttribute(value = "tranData") JSONObject tranData,
                               @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                               @RequestAttribute(value = "uri") String uri,
                               @RequestAttribute(value = "commonParam") CommonParam commonParam,
                               @RequestAttribute(value = "startDate") Date startdate,
                               @RequestAttribute(value = "ip") String ip) throws ApiException {

        String randomCode = tranData.getString("randomCode");

        ResponseVo responseVo = merchantApplyInfoService.getApplyInfo(randomCode);
        return ApiUtil.dealReturnJson(responseVo, appEncrypt, startdate, uri);
    }

    /**
     * 手机号 鉴权登陆
     */
    @RequestMapping(value = "/getOssInfo", method = {RequestMethod.POST, RequestMethod.GET})
    public String getOssInfo(@RequestAttribute(value = "tranData") JSONObject tranData,
                             @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                             @RequestAttribute(value = "uri") String uri,
                             @RequestAttribute(value = "commonParam") CommonParam commonParam,
                             @RequestAttribute(value = "startDate") Date startdate,
                             @RequestAttribute(value = "ip") String ip) throws ApiException {

        String randomCode = tranData.getString("randomCode");

        ResponseVo responseVo = merchantApplyInfoService.getOssInfo(randomCode);
        return ApiUtil.dealReturnJson(responseVo, appEncrypt, startdate, uri);
    }


    /**
     * 提交申请信息
     */
    @RequestMapping(value = "/submitApplyInfo", method = {RequestMethod.POST, RequestMethod.GET})
    public String submitApplyInfo(@RequestAttribute(value = "tranData") JSONObject tranData,
                                  @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                                  @RequestAttribute(value = "tranDataBean") ApplyInfoBean applyInfoBean,
                                  @RequestAttribute(value = "uri") String uri,
                                  @RequestAttribute(value = "commonParam") CommonParam commonParam,
                                  @RequestAttribute(value = "startDate") Date startdate,
                                  @RequestAttribute(value = "ip") String ip) throws ApiException {

        ResponseVo responseVo = merchantApplyInfoService.submitApplyInfo(applyInfoBean);
        return ApiUtil.dealReturnJson(responseVo, appEncrypt, startdate, uri);
    }

}
